package com.baway.biliclitent.fragment.TomatoType;

import android.graphics.Color;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;

import com.baway.biliclitent.R;
import com.baway.biliclitent.adapter.MyRecoAdapter;
import com.baway.biliclitent.adapter.MySeasonRecyAdapter;
import com.baway.biliclitent.adapter.MySeleRecyAdapter;
import com.baway.biliclitent.custom.NoScrollViewPager;
import com.baway.biliclitent.model.DetailsBean;
import com.baway.biliclitent.model.Details_Recom_Bean;
import com.baway.biliclitent.util.OkHttpUtils;
import com.bumptech.glide.Glide;
import com.flyco.tablayout.SlidingTabLayout;

import java.io.UnsupportedEncodingException;
import java.util.List;

import jp.wasabeef.glide.transformations.BlurTransformation;

import static com.baway.biliclitent.R.id.view_pager;


/**
 * 番剧详情界面
 */
public class TomatoDetails extends AppCompatActivity {
    //首页番剧头
    public static final String DRAMA_HEAD = "http://bangumi.bilibili.com/";
    //番剧详情 番剧推荐
    public static final  String DRAMA_DETAILS_RECOMMEND = "api/season/recommend/5070.json?access_key=19946e1ef3b5cad1a756c475a67185bb&actionKey=appkey&appkey=27eb53fc9058f8c3&build=3940&device=phone&mobi_app=iphone&platform=ios&season_id=5070&sign=744e3a3f52829e4344c33908f7a0c1ef&ts=1477898527";
    //番剧详情
    public static final String DRAMA_DETAILS = "api/season_v4?access_key=19946e1ef3b5cad1a756c475a67185bb&actionKey=appkey&appkey=27eb53fc9058f8c3&build=3940&device=phone&mobi_app=iphone&platform=ios&season_id=5070&sign=3e5d4d7460961d9bab5da2341fd98dc1&ts=1477898526&type=bangumi";
    private ImageView mbu_big_bg;
    private ImageView mbun_image;
    private TextView mBtitle;
    private TextView mBupdate;
    private TextView mBplay;
    private RecyclerView mSeasonRecy;
    private TextView mBindex;
    private RecyclerView mSeleRecy;
    private TextView mDetailsText;
    private RecyclerView mBrecomRecy;
    private NestedScrollView mScroll;
    private MySeasonRecyAdapter mySeasonRecyAdapter;
    private MySeleRecyAdapter mySeleRecyAdapter;
    private MyRecoAdapter myRecoAdapter;
    private Toolbar toBar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tomato_details);
        //初始化资源id
        initView();
        //获取网络信息
        getData();
    }
    //获取网络信息
    private void getData() {
        OkHttpUtils httpUtils = OkHttpUtils.getHttpUtils();
        httpUtils.loadDataFromNet(DRAMA_HEAD + DRAMA_DETAILS, DetailsBean.class, new OkHttpUtils.CallBackListener<DetailsBean>() {
            @Override
            public void onSuccess(DetailsBean result) {
                //设置标题
                mBtitle.setText(result.getResult().getTitle());
                //设置完结数
                mBupdate.setText("已完结"+returnBM(result.getResult().getNewest_ep_index())+"话");
                //设置播放数和追番数
                mBplay.setText("播放："+returnBM(result.getResult().getPlay_count())+"  追番："+returnBM(result.getResult().getFavorites()));
                //加载小图
                Glide.with(TomatoDetails.this).load(returnBM(result.getResult().getCover())).into(mbun_image);
                //加载大图
                Glide.with(TomatoDetails.this).load(returnBM(result.getResult().getCover())).bitmapTransform(new BlurTransformation(TomatoDetails.this)).into(mbu_big_bg);
                //设置简介
                mDetailsText.setText(result.getResult().getEvaluate());
                //设置多少话
                mBindex.setText(returnBM(result.getResult().getTotal_count())+"全");
                //取出集合 设置对应的适配器
                List<DetailsBean.ResultBean.SeasonsBean> seasons = result.getResult().getSeasons();
                mySeasonRecyAdapter.setData(seasons);
                List<DetailsBean.ResultBean.EpisodesBean> episodes = result.getResult().getEpisodes();
                mSeleRecy.scrollToPosition(episodes.size()-1);
                mySeleRecyAdapter.setData(episodes);
            }
            @Override
            public void onFail() {

            }
        });
        //解析下方的番剧推荐
        httpUtils.loadDataFromNet(DRAMA_HEAD + DRAMA_DETAILS_RECOMMEND, Details_Recom_Bean.class, new OkHttpUtils.CallBackListener<Details_Recom_Bean>() {
            @Override
            public void onSuccess(Details_Recom_Bean result) {
                //取出集合
                List<Details_Recom_Bean.ResultBean.ListBean> list = result.getResult().getList();
                myRecoAdapter.setData(list);
            }

            @Override
            public void onFail() {

            }
        });
    }

    //初始化资源id
    private void initView() {
        toBar = (Toolbar) findViewById(R.id.toolbar);
        toBar.setTitle("番剧详情");
        setSupportActionBar(toBar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        //整体的滑动布局
        mScroll = (NestedScrollView) findViewById(R.id.bungum_scroll);
        //上方背景大图
        mbu_big_bg = (ImageView) findViewById(R.id.buggu_im_bg);
        //小的图片
        mbun_image = (ImageView) findViewById(R.id.bun_image);
        //漫画标题
        mBtitle = (TextView) findViewById(R.id.bangumi_title);
        //漫画完结
        mBupdate = (TextView) findViewById(R.id.bangumi_update);
        //漫画追番人数
        mBplay = (TextView) findViewById(R.id.bangumi_play);
        //一共有多少话
        mBindex = (TextView) findViewById(R.id.tv_update_index);
        //简介
        mDetailsText = (TextView) findViewById(R.id.bangumi_details_introduction);
        //Tv与日播精选的RecyclerView
        mSeasonRecy = (RecyclerView) findViewById(R.id.bangumi_seasons_recycler);
        //番剧推荐的RecyclerView
        mBrecomRecy = (RecyclerView) findViewById(R.id.bangumi_recommend_recycler);
        //目录的recyclerView
        mSeleRecy = (RecyclerView) findViewById(R.id.bangumi_selection_recycler);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        //设置为横向滚动
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mSeleRecy.setLayoutManager(linearLayoutManager);
        //设置管理者
        LinearLayoutManager linM = new LinearLayoutManager(this);
        linM.setOrientation(LinearLayoutManager.HORIZONTAL);
        mSeasonRecy.setLayoutManager(linM);
        mBrecomRecy.setLayoutManager(new LinearLayoutManager(this){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        //设置适配器
        mySeasonRecyAdapter = new MySeasonRecyAdapter(this);
        mySeleRecyAdapter = new MySeleRecyAdapter(this);
        myRecoAdapter = new MyRecoAdapter(this);
        mSeasonRecy.setAdapter(mySeasonRecyAdapter);
        mSeleRecy.setAdapter(mySeleRecyAdapter);
        mBrecomRecy.setAdapter(myRecoAdapter);
        //设置条目移除的动画
        mSeasonRecy.setItemAnimator(new DefaultItemAnimator());
        mSeleRecy.setItemAnimator(new DefaultItemAnimator());
        mBrecomRecy.setItemAnimator(new DefaultItemAnimator());
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }
    //转化编码格式
    private String returnBM(String str){
        try {
            String mStr = new String(str.getBytes("ISO-8859-1"),"UTF-8");
            return mStr;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }



}
